import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:todo_app/components/Overlay/main.dart';
import 'package:todo_app/utils/call/CallHelper.dart';
import 'package:todo_app/utils/call/states/ConnectingState.dart';
import 'package:todo_app/utils/call/states/TalkState.dart';

import '../../hooks/CallFmsController.dart';
import 'section/CallInSection.dart';
import 'section/CallOutSection.dart';
import 'section/TalkSection.dart';

OverlayEntry showSingleChatRoom() {
  return showOverlay(builder: (BuildContext _, OverlaySupportState state) {
    return const RTCSingleRoomPage();
  });
}

class RTCSingleRoomPage extends HookWidget {
  const RTCSingleRoomPage({super.key});

  @override
  Widget build(BuildContext context) {
    useCallFmsController();
    var curState = CallHelper.callFms.curState;
    bool connected = curState is TalkState;
    bool connecting = curState is ConnectingState;

    if(connected) {
      var room = curState.context?.room;
      if(room != null) {
        return TalkSection(room);
      }
    }

    if(connecting) {
      var room = curState.context?.room;
      if(room != null) {
        var callIsMe = room.user_id == room.created_by;
        if(callIsMe) {
          return CallOutSection(room);
        } else {
          return CallInSection(room);
        }
      }
    }
    return Container();
  }
}